//
//  UserInfoSettingViewController.m
//  CarLifes
//
//  Created by yu on 2017/6/23.
//  Copyright © 2017年 jpas. All rights reserved.
//

#import "UserInfoSettingViewController.h"
#import "CLModifyEmailViewController.h"

#define kMeCellId @"MeCellId"

@interface UserInfoSettingViewController ()<UITableViewDelegate,UITableViewDataSource,UIImagePickerControllerDelegate,
UINavigationControllerDelegate>

@property (weak, nonatomic) IBOutlet UITableView *tableView;
@property (nonatomic, strong) NSArray *titles;
@property (nonatomic, strong) UIImageView *iconImgView;
@property (nonatomic, strong) UIImagePickerController *imagePicker;

@end

@implementation UserInfoSettingViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.title = @"账户资料";
    [self setWhiteNavigationBar];
    [self setLeftImageNamed:@"back_gray" action:@selector(back)];
}

- (void)setTableView:(UITableView *)tableView
{
    _tableView = tableView;
    if (@available(iOS 13.0, *)) {
        UIColor *bgColor = [UIColor colorWithDynamicProvider:^UIColor * _Nonnull(UITraitCollection * _Nonnull traitCollection) {
            if (traitCollection.userInterfaceStyle == UIUserInterfaceStyleLight) {
                return kBackGroundLightGrayColor;
            } else {
                return UIColor.systemBackgroundColor;
            }
        }];
        _tableView.backgroundColor = bgColor;
    } else {
        _tableView.backgroundColor = kBackGroundLightGrayColor;
    }
}

- (UIImageView *)iconImgView {
    if (!_iconImgView) {
        _iconImgView = [[UIImageView alloc] init];
        _iconImgView.width_sd = 50;
        _iconImgView.height_sd = 50;
        _iconImgView.layer.masksToBounds = YES;
        _iconImgView.layer.cornerRadius = 25.f;
        _iconImgView.image = IMAGE_NAMED(@"IMG_2368.JPG");
    }
    // 此处刷新头像
    
    return _iconImgView;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.titles.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return indexPath.row == 0 ? 45 : 45;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:kMeCellId];
    
    if (!cell) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:kMeCellId];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
      
        UILabel *lineView = [UILabel new];
        [cell.contentView addSubview:lineView];
        
        lineView.sd_layout
        .bottomSpaceToView(cell.contentView, 0)
        .leftSpaceToView(cell.contentView, 15)
        .rightSpaceToView(cell.contentView, 15)
        .heightIs(1);
        
        
        
        // 箭头
//        if (!(indexPath.row == 0 || indexPath.row == 1)) {
//
//            UIImageView *arrowImgView = [UIImageView new];
//            arrowImgView.image = IMAGE_NAMED(@"arrow_right");
//            arrowImgView.contentMode = UIViewContentModeScaleAspectFit;
//            [cell.contentView addSubview:arrowImgView];
//
//            arrowImgView.sd_layout.rightEqualToView(lineView).centerYEqualToView(cell.contentView).widthIs(8).heightIs(14);
//            [cell.detailTextLabel setSingleLineAutoResizeWithMaxWidth:250];
//            cell.detailTextLabel.sd_layout
//            .rightSpaceToView(arrowImgView, 5)
//            ;
//        }
        
        if (@available(iOS 13.0, *)) {
            UIColor *lineColor = [UIColor colorWithDynamicProvider:^UIColor * _Nonnull(UITraitCollection * _Nonnull traitCollection) {
                if (traitCollection.userInterfaceStyle == UIUserInterfaceStyleLight) {
                    return kBackGroundLightGrayColor;
                } else {
                    return UIColor.opaqueSeparatorColor;
                }
            }];
            lineView.backgroundColor = lineColor;
            
            UIColor *cellColor = UIColor.secondarySystemGroupedBackgroundColor;
            cell.contentView.backgroundColor = cellColor;
        
            cell.textLabel.textColor = [UIColor.labelColor colorWithAlphaComponent:0.8];
        } else {
            lineView.backgroundColor = kBackGroundLightGrayColor;
            cell.contentView.backgroundColor = UIColor.whiteColor;
            cell.textLabel.textColor = kTextBlackColor;
        }
        
        cell.textLabel.font = SYSTEM_FONT(16);
        cell.textLabel.text = _titles[indexPath.row];
        //cell.textLabel.textColor = kTextBlackColor;
        cell.detailTextLabel.font = SYSTEM_FONT(14);
        
    }
    
    if (indexPath.row == 0) {
//        cell.accessoryView = self.iconImgView;
        cell.detailTextLabel.text = self.user.account;
    } else if (indexPath.row == 1) {
        cell.detailTextLabel.text = self.user.nickName;
    } else if (indexPath.row == 2) {
        cell.detailTextLabel.text = self.user.mobile;
    } else if (indexPath.row == 3) {
        cell.detailTextLabel.text = self.user.email;
    }
//    else if (indexPath.row == 4) {
//        cell.detailTextLabel.text = self.user.email;
//    }
    
    return cell;
}

//- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
//    [tableView deselectRowAtIndexPath:indexPath animated:YES];
//
//    if (indexPath.row == 0) {
//        // 更新头像
//        [self uploadIcon];
//    } else if (indexPath.row == 2) {
//        // 昵称
//        CLModifyNicknameViewController *nickname = [[CLModifyNicknameViewController alloc] init];
//        [self.navigationController pushViewController:nickname animated:YES];
//    } else if (indexPath.row == 3) {
//        // 电话
//        CLModifyTelephoneViewController *telephone = [[CLModifyTelephoneViewController alloc] init];
//        [self.navigationController pushViewController:telephone animated:YES];
//
//    } else if (indexPath.row == 4) {
//        // 邮箱
//        CLModifyEmailViewController *email = [[CLModifyEmailViewController alloc] init];
//        [self.navigationController pushViewController:email animated:YES];
//    }
//}

- (void)uploadIcon
{
    UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"更改头像" message:@"选择头像位置" preferredStyle:UIAlertControllerStyleActionSheet];
    UIAlertAction *fromCamera = [UIAlertAction actionWithTitle:@"从相机选取" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        self.imagePicker.sourceType = UIImagePickerControllerSourceTypeCamera;
        self.imagePicker.allowsEditing = YES;
        [self presentViewController:self.imagePicker animated:YES completion:nil];
    }];
    
    fromCamera.enabled = [UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera];
    
    UIAlertAction *fromGallery = [UIAlertAction actionWithTitle:@"从相册选取" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        self.imagePicker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
        [self presentViewController:self.imagePicker animated:YES completion:nil];
    }];
    
    UIAlertAction *cancel = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil];
    
    
    [alertController addAction:fromGallery];
    [alertController addAction:fromCamera];
    [alertController addAction:cancel];
    [self presentViewController:alertController animated:YES completion:nil];
}

//- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
//    return 70;
//}

//- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
//
//    UIView *footerView = [UIView new];
//
//    UIButton *logoutBtn = [UIButton new];
//    logoutBtn.backgroundColor = [UIColor whiteColor];
//    logoutBtn.titleLabel.font = SYSTEM_FONT(16);
//    [logoutBtn setTitle:@"退出当前帐号" forState:UIControlStateNormal];
//    [logoutBtn setTitleColor:[UIColor baseCLBlueColor] forState:UIControlStateNormal];
//
//    [footerView addSubview:logoutBtn];
//    logoutBtn.sd_layout
//    .leftEqualToView(footerView)
//    .rightEqualToView(footerView)
//    .bottomEqualToView(footerView)
//    .heightIs(40);
//
//    return footerView;
//}

#pragma mark - ImagePicker Delegate
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString *,id> *)info {
    [picker dismissViewControllerAnimated:YES completion:nil];
    UIImage *avatar;
    if (picker.sourceType == UIImagePickerControllerSourceTypePhotoLibrary) {
        avatar = [info objectForKey:UIImagePickerControllerOriginalImage];
    } else if (picker.sourceType == UIImagePickerControllerSourceTypeCamera) {
        avatar = [info objectForKey:UIImagePickerControllerEditedImage];
    }
    self.iconImgView.image = avatar;
}


- (void)back {
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    [self.tableView reloadData];
    [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleDefault animated:YES];
    
//    PPLog(@"mobile:%@  name:%@  email:%@",self.user.mobile,self.user.nickName,self.user.email);
}

- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleLightContent animated:YES];
}

- (UIImagePickerController *)imagePicker {
    if (!_imagePicker) {
        _imagePicker = [[UIImagePickerController alloc] init];
        _imagePicker.delegate = self;
    }
    return _imagePicker;
}

- (NSArray *)titles {
    if (!_titles) {
        _titles = @[@"帐号",@"昵称",@"电话",@"邮箱"];
    }
    return _titles;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}



@end
